package main

import (
	"github.com/goburrow/modbus"
	"log"
	"time"
)

func main() {
	handler := modbus.NewTCPClientHandler("127.0.0.1:502")
	handler.Timeout = 5 * time.Second
	handler.SlaveId = 1
	err := handler.Connect()
	if err != nil {
		log.Fatal(err)
	}
	defer handler.Close()

	client := modbus.NewClient(handler)
	for {
		results, err := client.ReadHoldingRegisters(0, 10)
		if err != nil {
			log.Println(err)
			for {
				err := handler.Connect()
				if err == nil {
					break
				}
				time.Sleep(1 * time.Second)
			}
		}
		log.Printf("Received: %v", results)
		time.Sleep(1 * time.Second)
	}

}
